<?php
require_once(dirname(__FILE__) . '/../../includes/spip_launcher.php');
include_spip('functions/pdk_common');
require_once(_DIR_PLUGIN_PDK.'classes/simpletest/class.PdkModelTest.php');

class TestClassCurrencyModel extends PdkModelTest {
    /**
     *  RubriqueCurrencyModel
     */
    protected $rub_cur_mdl;
    
    /**
     *  PdkArticleModel
     */
    protected $rub_mdl;
    
    /**
     *  PdkRubriqueModel
     */
    protected $art_mdl;
    
    /**
     *  Current currency ID wich is stored in $_SESSION
     */
    private $session_current_currency_id;
    
    public function __construct(){
        parent::__construct();
        
        // Remember curent currency ID
        @session_start();
        $this->session_current_currency_id = $_SESSION["current_currency_id"];
        
        // Define all additional models:
        $pdk_models = array();
        $this->rub_cur_mdl = pdk_load_class('RubriqueCurrencyModel','classes');
        $pdk_models['RubriqueCurrencyModel'] = $this->rub_cur_mdl;
        $this->rub_mdl = pdk_load_class('PdkRubriqueModel','classes/models');
        $pdk_models['PdkRubriqueModel'] = $this->rub_mdl;        
        $this->art_mdl = pdk_load_class('PdkArticleModel','classes/models');
        $pdk_models['PdkArticleModel'] = $this->art_mdl;
        
        $this->AddPdkModels($pdk_models);
        
        // Set main (tested) model:
        $this->SetPdkModel('CurrencyModel', pdk_load_class('CurrencyModel', 'classes'));
        
        $this->dropTestTables = ECATALOGUE_TESTS_DROP_TEST_TABLES;
        
        // Задаем вручную валюту по умолчанию, по той же причине, по которой не кактит тест в функции Test_CurrentCurrency
        $this->model->SetCurrentCurrencyId(2); 
    }
    
    /**
     *  Run before each test
     */
    function setUp() {
        parent::setUp();    
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        parent::tearDown();
    }
    
    function __destruct() {
        // Call parent destructor to delete created tables
        parent::__destruct();
        
        // Restore current currency ID
        $_SESSION["current_currency_id"] = $this->session_current_currency_id;
    }
    
    function testGetCurrencyByIdRubrique() {
        // Here we clear standart test data and add our own
        $this->test_table->ClearTables($this->rub_cur_mdl);
        $data = array('id_rubrique'=>1, 'id_currency'=>1);
        $this->rub_cur_mdl->insert($data);
        
        // Rubrique and currency have direct link
        $currency_sel = $this->model->GetCurrencyByIdRubrique(1);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // Currency inheritance from parent rubrique        
        // rubrique 2 lies in  rubrique 1, so it must have the same currency
        $currency_sel = $this->model->GetCurrencyByIdRubrique(2);        
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // Deeper inheritance
        // rubrique 3 lies in rubrique 1 branch, so it must have the same currency
        $currency_sel = $this->model->GetCurrencyByIdRubrique(3);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // No currency - current currency must be returned
        // Rubrique 4 hasn't got any currency
        $currency_sel = $this->model->GetCurrencyByIdRubrique(4);        
        //$this->assertFalse($currency_sel);
        $this->assertEqual($this->model->GetCurrentCurrency(), $currency_sel);
    }  
    
    function testGetCurrencyByIdArticle() {
        // Here we clear standart test data and add our own
        $this->test_table->ClearTables($this->rub_cur_mdl);        
        $data = array('id_rubrique'=>1, 'id_currency'=>1);
        $this->rub_cur_mdl->insert($data);
                
        // Article parent rubrique and currency have direct link
        $currency_sel = $this->model->GetCurrencyByIdArticle(1);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // Currency inheritance from parent rubrique        
        // rubrique 2 lies in  rubrique 1, so article wich lies in rubrique 2 must have the same currency
        $currency_sel = $this->model->GetCurrencyByIdArticle(2);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // Deeper inheritance
        // rubrique 3 lies in rubrique 1 branch, so article wich lies in rubrique 3 must have the same currency
        $currency_sel = $this->model->GetCurrencyByIdArticle(3);
        $this->assertEqual($this->model->test_data[0], $currency_sel);
        
        // No currency - current currency must be returned
        // Rubrique 4 hasn't got any currency so article wich lies in rubrique 4
        $currency_sel = $this->model->GetCurrencyByIdArticle(4);
        //$this->assertFalse($currency_sel);
        $this->assertEqual($this->model->GetCurrentCurrency(), $currency_sel);
    }
    
    public function testGetCurrencySignByIdArticle() {
        // Here we clear standart test data and add our own
        $this->test_table->ClearTables($this->rub_cur_mdl);        
        $data = array('id_rubrique'=>1, 'id_currency'=>1);
        $this->rub_cur_mdl->insert($data);
        
        // article 1 lies in rubrique 1, so it has currency 1 signs 
        $sign_sel = $this->model->GetCurrencySignByIdArticle(1);
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdArticle(1,'sign_pre');
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdArticle(1,'sign_after');
        $this->assertEqual($this->model->test_data[0]['sign_after'], $sign_sel);
        
        // article 2 lies in rubrique 2, wich lies in rubrique 1, so article 1 has currency 1 signs
        $sign_sel = $this->model->GetCurrencySignByIdArticle(2);
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdArticle(2,'sign_pre');
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdArticle(2,'sign_after');
        $this->assertEqual($this->model->test_data[0]['sign_after'], $sign_sel);
    }
    
    function Test_CurrentCurrency() {
        // Default current currency must be 2-nd currency (because of rate=1):
        // Этот тест не катити, потому что в конструкторе кто то создает нашу модель раньше чем мы сами, и текущая валюта выставляется по реальной тиблице. Надо както пофиксить этот трабл                
        //$this->assertEqual($this->model->test_data[1], $this->model->GetCurrentCurrency());
        
        $this->model->SetCurrentCurrencyId(1);        
        $this->assertEqual($this->model->test_data[0], $this->model->GetCurrentCurrency());
    }
    
    function TestConvertPriceToCurrentCurrency() {
        $this->model->SetCurrentCurrencyId($this->model->test_data[0]['id_currency']);
        $this->assertEqual(100*$this->model->test_data[0]['rate'], $this->model->ConvertPriceToCurrentCurrency(100));
        
        $this->model->SetCurrentCurrencyId($this->model->test_data[1]['id_currency']);
        $this->assertEqual(100*$this->model->test_data[1]['rate'], $this->model->ConvertPriceToCurrentCurrency(100));
    }
    
    function TestConvertPrice() {
        $currency_from = $this->model->test_data[0];
        $currency_to = $this->model->test_data[1];
        $price = $this->model->ConvertPrice(100, $currency_from['id_currency'], $currency_to['id_currency']);
        $this->assertEqual(100*$currency_to['rate']/$currency_from['rate'], $price);
        
        // if destination currency isn't given, price must be converted into current currency
        $currency_to = $this->model->GetCurrentCurrency();
        $price = $this->model->ConvertPrice(100, $currency_from['id_currency']);
        $this->assertEqual(100*$currency_to['rate']/$currency_from['rate'], $price);
    }
    
    /*
    function testGetCurrencySignByIdCurrency() {
        // Insert test currencies
        $this->model->insert($this->model->test_data[0]);
        
        $sign_sel = $this->model->GetCurrencySignByIdCurrency(1);
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdCurrency(1,'sign_pre');
        $this->assertEqual($this->model->test_data[0]['sign_pre'], $sign_sel);
        $sign_sel = $this->model->GetCurrencySignByIdCurrency(1,'sign_after');
        $this->assertEqual($this->model->test_data[0]['sign_after'], $sign_sel);
    }*/
}
